package com.dodo.blog.ui.component.input;

import com.dodo.blog.ui.component.HtmlComponent;

/**
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
public class InputComponent
        extends HtmlComponent
{
    private static final long serialVersionUID = 1L;

    public enum InputType
    {
        TEXT( "text" ),
        PASSWORD( "password" ),
        CHECKBOX( "checkbox" ),
        RADIO( "radio" ),
        SUBMIT( "submit" );

        String name;

        InputType( String name )
        {
            this.name = name;
        }

        public String getName()
        {
            return name;
        }
    }

    public InputComponent( String id, InputType type )
    {
        this( id, type.getName() );
        setName( id );
    }

    public InputComponent( String id, String type )
    {
        super( Tag.INPUT, Type.INLINE );

        setAttribute( Attribute.TYPE, type );
        setId( id );
    }

    public InputComponent setReadOnly()
    {
        setAttribute( Attribute.READONLY );
        return this;
    }

    public InputComponent setDisabled()
    {
        setAttribute( Attribute.DISABLED );
        return this;
    }

    public InputComponent setPlaceHolder( String placeHolder )
    {
        setAttribute( Attribute.PLACEHOLDER, placeHolder );
        return this;
    }

    public InputComponent setAutoFocus( boolean autoFocus )
    {
        if ( autoFocus )
        {
            setAttribute( Attribute.AUTOFOCUS );
        }
        else
        {
            removeAttribute( Attribute.AUTOFOCUS );
        }

        return this;
    }
}
